var form;
var tree;
var tableIns;
var ctxPath = '/'
$(function () {
    layui.config({
        base: ctxPath + 'lib/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index'//主入口模块
    }).use(['index', 'table', 'form', 'tree'], function () {
        var table = layui.table;
        form = layui.form;
        tree = layui.tree;
        // 渲染表格
        tableIns = table.render({
            elem: '#table',
            toolbar: '#table-title',
            url: 'serverList',
            cols: [[
                {type: 'numbers'},
                {field: 'serverip', title: '存储服务器IP', align: 'center'},
                {field: 'serverport', title: '应用程序端口', align: 'center'},
                {field: 'cyqxh', title: '下载机构', align: 'center'},
                {field: 'status', title: '服务器状态', align: 'center'},
                {field: 'right', title: '操作', toolbar: '#table-toolbar', align: 'center'},
            ]]
        });
        buildOrganTree();
        //监听行工具事件
        table.on('tool(table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                openEdit(data)
            } else if (obj.event === 'del') {
                openDel(data);
            }
        });
    });
})

function openEdit(data) {
    layer.open({
        type: 1,
        title: "新建服务器配置",
        fixed: false,
        resize: false,
        shadeClose: true,
        area: ['550px', '500px'],
        content: $('#clusterEdit'),
        btn: ['确定', '取消'],
        success: function () {
            //填充表单
            form.val('clusterForm', data);
            renderForm()
        }, btn1: function () {
            clusterSubmit("1");
        }, end: function () {
            cleanForm("clusterForm")
            tableReload();
        }
    });
}

function openAdd() {
    layer.open({
            type: 1,
            title: "新建服务器配置",
            fixed: false,
            resize: false,
            shadeClose: true,
            area: ['550px', '500px'],
            content: $('#clusterEdit'),
            btn: ['确定', '取消'],
            btn1: function () {
                clusterSubmit("0");
            },
            end: function () {
                cleanForm("clusterForm")
                tableReload();
            }
        }
    )
}

function clusterSubmit(oper) {
    var type = oper == "0" ? "POST" : "PUT";
    $.ajax({
        type: type,
        data: $("#clusterForm").serialize(),
        url: ctxPath + "config/cluster/clusterInfo"
    });
}

/**
 * 删除配置
 * @param obj
 */
function openDel(obj) {
    layer.confirm('您确定要删除服务器：' + obj.serverip + '么', function () {
        $.ajax({
            type: "DELETE",
            data: {
                "id": obj.id
            },
            url: ctxPath + "config/cluster/clusterInfo",
            complete: function () {
                tableReload()
            }
        });
    });
}

function tableReload() {
    tableIns.reload({
        page: {
            curr: 1 //重新从第 1 页开始
        }
    });
}

function chooseOrgan() {
    layer.open({
            type: 1,
            title: "选择下载机构",
            fixed: false,
            resize: false,
            shadeClose: true,
            area: ['450px', '400px'],
            content: $('#organTreeDiv'),
            btn: ['确定', '取消'],
            btn1: function (index) {
                tree = layui.tree;
                var checkData = tree.getChecked('treeId');
                var arr = getCheckedId(checkData);
                $("#cyqxh").val(arr)
                layer.close(index);
            }
        }
    )
}

function buildOrganTree() {
    $.ajax({
        type: "GET",
        url: ctxPath + "system/organization/treeInfo",
        success: function (result) {
            if (result.code == "1") {
                var data = result.obj
                tree.render({
                    elem: '#organTree'  //绑定元素
                    , showCheckbox: true
                    , data: data
                    , id: 'treeId'
                });
            } else {
                layer.alert(result.msg)
            }
        }
    });
}

function getCheckedId(jsonObj) {
    var id = "";
    $.each(jsonObj, function (index, item) {
        if (id !== "") {
            id = id + "," + item.id;
        } else {
            id = item.id;
        }
        var i = getCheckedId(item.children);
        if (i !== "") {
            id = id + "," + i;
        }
    });
    return id;
}